package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityHouseCalculatorBinding;
import flc.ast.fragment.HouseKind1Fragment;
import flc.ast.fragment.HouseKind2Fragment;
import flc.ast.fragment.HouseKind3Fragment;

public class HouseCalculatorActivity extends BaseAc<ActivityHouseCalculatorBinding> {

    private Fragment mCurShowFragment;
    private Fragment houseKind1Fragment,houseKind2Fragment,houseKind3Fragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_house_calculator;
    }

    @Override
    protected void initView() {
        mDataBinding.ivHouseCalculateBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvHouseKind1.setOnClickListener(this);
        mDataBinding.tvHouseKind2.setOnClickListener(this);
        mDataBinding.tvHouseKind3.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvHouseKind1:
                clearSelection();
                showHouseKind1Fragment();
                mDataBinding.tvHouseKind1.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvHouseKind1.setTextSize(18);
                mDataBinding.ivHouseKind1.setVisibility(View.VISIBLE);
                break;
            case R.id.tvHouseKind2:
                clearSelection();
                showHouseKind2Fragment();
                mDataBinding.tvHouseKind2.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvHouseKind2.setTextSize(18);
                mDataBinding.ivHouseKind2.setVisibility(View.VISIBLE);
                break;
            case R.id.tvHouseKind3:
                clearSelection();
                showHouseKind3Fragment();
                mDataBinding.tvHouseKind3.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvHouseKind3.setTextSize(18);
                mDataBinding.ivHouseKind3.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void initData() {
        showHouseKind1Fragment();
    }


    //显示商业贷款模块
    private void showHouseKind1Fragment() {
        if (houseKind1Fragment == null) {
            houseKind1Fragment = new HouseKind1Fragment();
        }
        showFragment(houseKind1Fragment);
    }

    //显示公积金模块
    private void showHouseKind2Fragment() {
        if (houseKind2Fragment == null) {
            houseKind2Fragment = new HouseKind2Fragment();
        }
        showFragment(houseKind2Fragment);
    }

    //显示组合贷款模块
    private void showHouseKind3Fragment() {
        if (houseKind3Fragment == null) {
            houseKind3Fragment = new HouseKind3Fragment();
        }
        showFragment(houseKind3Fragment);
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) return;
        if (fragment.isAdded() && fragment.isVisible()) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurShowFragment != null) {
            transaction.hide(mCurShowFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fl_container, fragment);
        } else {
            transaction.show(fragment);
        }
        mCurShowFragment = fragment;
        transaction.commitAllowingStateLoss();
    }

    private void clearSelection() {
        mDataBinding.tvHouseKind1.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvHouseKind1.setTextSize(15);
        mDataBinding.ivHouseKind1.setVisibility(View.GONE);
        mDataBinding.tvHouseKind2.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvHouseKind2.setTextSize(15);
        mDataBinding.ivHouseKind2.setVisibility(View.GONE);
        mDataBinding.tvHouseKind3.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvHouseKind3.setTextSize(15);
        mDataBinding.ivHouseKind3.setVisibility(View.GONE);
    }

}